/*
 *市场活动费用明细
 */
public class MarketingExpensesController
{
    public Campaign cp {get;set;} //市场活动
    public String strjson {get;set;}//传递的参数
    public List<MarketingExpenses__c> mes {get;set;}//市场活动费用
    public MarketingExpenses__c me {get;set;}//市场活动费用
    public Boolean isShouldEdit {get;private set;}//是否允许增删行
    public Boolean isShouldEditBudget {get;private set;}//是否允许编辑预算
    public Boolean isShouldEditCost {get;private set;}// 是否允许编辑执行
    public String deleteId {get;set;}//删除的Id
    
    public MarketingExpensesController(ApexPages.StandardController con)
    {
        cp =[select Id ,Status from Campaign where Id =: System.currentPageReference().getParameters().get('cpId')];
      // cp.Status = '执行审批中';
        isShouldEdit = (cp.Status == '草案中' || cp.Status == '预算审批已拒绝' || cp.Status == null) ? true :false;
        isShouldEditBudget = (cp.Status != '预算审批完毕' && cp.Status != '预算审批中' && cp.Status != '执行审批中' && cp.Status != '执行审批完毕' && cp.Status != '执行审批已拒绝')? true : false;
        isShouldEditCost = (cp.Status == '执行审批中' || cp.Status == '执行审批完毕')? false : true;
        
        
        mes = new List<MarketingExpenses__c>();
        mes = [select Id,Remark__c,Budget__c,Item__c,Cost__c from MarketingExpenses__c where MarketingActivity__c =:System.currentPageReference().getParameters().get('cpId')];
        me = new MarketingExpenses__c();
    }
    public void init(){

    }

    public PageReference save(){
    //System.debug(strjson);
        Savepoint sp = Database.setSavepoint();
        try{
           /* Map<String ,Id>recordtypeMap = new Map<String,Id>(); 
            for(PicklistEntry p: Campaign.sobjecttype.getdescribe().fields.getMap().get('Status').getdescribe().getPicklistValues()){
            	if(p.isActive() == true){
                	if(p.getValue() == '草案中	'){
                        recordtypeMap.put(p.getValue(),Utils.getRecordTypeIdByDevNameAndObjName('草案中','MarketingExpenses__c'));
                    }else if(p.getValue() == '执行审批完毕'){
                        recordtypeMap.put(p.getValue(),Utils.getRecordTypeIdByDevNameAndObjName('执行审批完毕','MarketingExpenses__c'));
                    }else if(p.getValue() == '预算审批完毕' || p.getValue() == '预算审批中'){
                        recordtypeMap.put(p.getValue(),Utils.getRecordTypeIdByDevNameAndObjName('预算审批中/完毕','MarketingExpenses__c'));
                    }
                    
                }
            }
            Id rid = null;
                  if(cp.Status == '草案中'){
                        rid = Utils.getRecordTypeIdByDevNameAndObjName('草案中','MarketingExpenses__c');
                    }else if(cp.Status == '执行审批完毕'){
                        rid = Utils.getRecordTypeIdByDevNameAndObjName('执行审批完毕','MarketingExpenses__c');
                    }else if(cp.Status == '预算审批完毕' || cp.Status == '预算审批中'){
                        rid = Utils.getRecordTypeIdByDevNameAndObjName('预算审批中/完毕','MarketingExpenses__c');
                    }*/
            ///添加的
            List<MarketingExpenses__c> mesadd = new List<MarketingExpenses__c>();
			if(strjson.length() > 0){
            List<Object> inJsonMap = (List<Object>)JSON.deserializeUntyped(strjson);
                for(Object obj : inJsonMap){
                     Map<String,Object> objmap=(Map<String,Object>)obj;
                     MarketingExpenses__c m = new MarketingExpenses__c();
                     m.MarketingActivity__c = cp.Id;
                     m.Item__c = String.valueOf(objmap.get('item'));
                     m.Budget__c = String.valueOf(objmap.get('budget')).length()>0 ? Decimal.valueOf(String.valueOf(objmap.get('budget'))) : 0;
                     m.Cost__c = String.valueOf(objmap.get('cost')).length()> 0 ?Decimal.valueOf(String.valueOf(objmap.get('cost'))) : 0;
                     m.Remark__c = String.valueOf(objmap.get('remark'));
                     //if(rid != null)m.RecordTypeId = rid;
                    
                     mesadd.add(m);
                  }
            
            }           
            ///删除的
          
            List<MarketingExpenses__c> deletMes = new List<MarketingExpenses__c>();
            List<MarketingExpenses__c> updateMes = new List<MarketingExpenses__c>();
            if(deleteId !=null && deleteId.length() > 0){
                Map<Id,Id>meMap = new Map<Id,Id>();
              for(String idStr : deleteId.split(',',-1)){
                    meMap.put(idStr,idStr);  
               }
                for(MarketingExpenses__c m :mes){
                if(meMap.containsKey(m.Id)){
                    deletMes.add(m);
                    }else{
                    updateMes.add(m);
                    }
                }
            }else{
            	updateMes.addAll(mes);
            }
              System.debug(deleteId + '\t'+updateMes + '\t' + mesadd);
            if(mesadd.size()> 0) insert mesadd;
            if(updateMes.size()> 0)update updateMes;
        if(deletMes.size() >0)delete deletMes;
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, '保存成功!'));
            return new PageReference('/'+cp.Id);
        }catch(Exception e){
        Database.rollback(sp);
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, e.getMessage()));
            return null;
        }
        
    }
    public PageReference cancel(){
            return new PageReference('/'+cp.Id);
    }
}